package com.wcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcy.mapper.SysPrivilegeMapper;
import com.wcy.domain.SysPrivilege;
import com.wcy.service.SysPrivilegeService;
import org.springframework.util.CollectionUtils;

/**
 * <p>Title:SysPrivilegeServiceImpl.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/18 22:14</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService{

    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;

    @Override
    public List<SysPrivilege> getAllSysPrivilege(Long menuId, Long roleId) {

        // 查询该菜单下所有的权限
        List<SysPrivilege> sysPrivileges = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId));
        if(CollectionUtils.isEmpty(sysPrivileges)){
            return Collections.emptyList();
        }
        // 当前角色是否包含该权限
        Set<Long> currentRoleSysPrivilegeIds=sysPrivilegeMapper.getPrivilegesByRoleId(roleId);
        for(SysPrivilege sysPrivilege:sysPrivileges){
            if(currentRoleSysPrivilegeIds.contains(sysPrivilege.getId())){
                sysPrivilege.setOwn(1);
            }
        }
        return sysPrivileges;
    }
}
